import pandas as pd
from py2neo import *
import numpy as np
from pprint import pprint
graph = Graph("http://127.0.0.1:7474",username="neo4j",password="neo4j")
df = pd.read_csv('ok.csv')

library = df['belong'].unique()



# 接下来是重新构造数据库
destroy = True
if __name__ == "__main__":
    if destroy == True:
        graph.delete_all()
        for lib in library:
            node = Node("library", name=lib)
            graph.create(node)
        matcher = NodeMatcher(graph)

        # for i in range(10):
        for i in range(len(df)):
            # 创建节点
            node_name = Node("function_name", name=df.loc[i, 'name'])
            node_description = Node("function_description", name=df.loc[i, 'description'])
            node_declaration = Node("function_declaration", name=df.loc[i, 'declaration'])
            node_returned = Node("function_returned", name=df.loc[i, "returned"])
            node_example = Node("function_example", name=df.loc[i, 'example'])

            graph.create(node_name)
            graph.create(node_description)
            graph.create(node_declaration)
            graph.create(node_returned)
            graph.create(node_example)

            node_lib = matcher.match("library", name=df.loc[i, 'belong']).first()

            relations = []
            relations.append(Relationship(node_lib, "包含", node_name))
            relations.append(Relationship(node_name, "属于", node_lib))
            relations.append(Relationship(node_name, "描述", node_description))
            relations.append(Relationship(node_name, "声明", node_declaration))
            relations.append(Relationship(node_name, "返回", node_returned))
            relations.append(Relationship(node_name, "例子", node_example))

            for relation in relations:
                graph.create(relation)
        print('no')

    res = graph.run("match (n:library) return n")
    for i in res.data():
        pprint((i['n']['name']))








